﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Av.Utils;

namespace WebUtils
{
    public class SymI18nMessage
    {

        public string Key { get; set; }

        public string Translation { get; set; }

        /// <summary>
        /// Culture in which translated - en, ru
        /// </summary>
        public string TranslationCulture { get; set; }

        /// <summary>
        /// Where it was found
        /// </summary>
        public string Filename { get; set; }

        /// <summary>
        /// Position in file (testing)
        /// </summary>
        public int Position { get; set; }

        /// <summary>
        /// How many times this value was used
        /// </summary>
        public int UsedInFiles { get; set; }

        public string ModuleName
        {
            get
            {
                string name = "";
                FileInfo fi = new FileInfo(Filename);
                //Log4cs.Log(Importance.Debug, "");
                if( fi.Directory.Name == "templates" )
                {
                    name = fi.Directory.Parent.Name;
                }
                return name;
            }
        }

        public SymI18nMessage(string key, string translation, string culture, string filename, int position)
        {
            UsedInFiles = 1;
            Key = key;
            Translation = translation;
            TranslationCulture = culture;
            Filename = filename;
            Position = position;
        }
    }

    public class SymI18nMessageHolder
    {
        /// <summary>
        /// Unsorted list of messages
        /// </summary>
        protected List<SymI18nMessage> m_arMessages = new List<SymI18nMessage>();

        /// <summary>
        /// Messages separated by module
        /// </summary>
        protected Dictionary<string, List<SymI18nMessage>> m_arModuleMessages = new Dictionary<string, List<SymI18nMessage>>();

        public int Total { get { return m_arMessages.Count; } }

        public void Add(string key, string translation, string culture, string filename, int position)
        {
            SymI18nMessage msg = new SymI18nMessage(key, translation, culture, filename, position);
            m_arMessages.Add(msg);
            if( !m_arModuleMessages.ContainsKey(msg.ModuleName) )
            {
                m_arModuleMessages[msg.ModuleName] = new List<SymI18nMessage>();
            }
            m_arModuleMessages[msg.ModuleName].Add(msg);
        }

        public string GetAsXml()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Module,Source,LT,RU,EN,DE");

            string prevModuleName = "_________";
            // Loop all modules
            foreach(var kvp in m_arModuleMessages)
            {
                // Loop messages in each module
                //sb.AppendLine(kvp.Key.ToUpper());
                foreach(var msg in kvp.Value)
                {
                    if(prevModuleName != kvp.Key)
                    {
                        sb.AppendFormat("{0},\"{1}\",,,", kvp.Key, msg.Key).AppendLine();
                        prevModuleName = kvp.Key;
                    } else
                    {
                        sb.AppendFormat(",\"{0}\",,,", msg.Key).AppendLine();
                    }
                }
            }

            return sb.ToString();
        }

        public void Normalize()
        {
            // Loop all modules
            foreach(var kvp in m_arModuleMessages)
            {
                //Log4cs.Log("Module [{0}] contains {1} messages before normalization...", kvp.Key, kvp.Value.Count);
                // Store messages to ensure uniqueness
                Dictionary<string, SymI18nMessage> moduleMessages = new Dictionary<string, SymI18nMessage>();
                // Loop messages in each module
                foreach(var msg in kvp.Value)
                {
                    if( !moduleMessages.ContainsKey(msg.Key) )
                    {
                        moduleMessages[msg.Key] = msg;
                    }
                }  // END FOREACH ( messages in module )

                m_arModuleMessages[kvp.Key].Clear();
                m_arModuleMessages[kvp.Key].AddRange(moduleMessages.Values.ToArray());
                //Log4cs.Log("Module [{0}] contains {1} messages after normalization...", kvp.Key, m_arModuleMessages[kvp.Key].Count);
            }
        }

        /// <summary>
        /// Returns list of all modules
        /// </summary>
        /// <returns></returns>
        public string[] GetModuleList()
        {
            return m_arModuleMessages.Keys.ToArray();
        }
    }
}
